package com.lipeng.controller;

import com.lipeng.bean.Shop;
import com.lipeng.bean.User;
import com.lipeng.service.ShopService;
import com.lipeng.utils.AddPic;
import com.lipeng.utils.CopyPic;
import com.lipeng.utils.StrUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * Created by dllo on 18/3/7.
 */
@Controller
public class ShopController {
    @Resource
    private ShopService shopService;

    @RequestMapping(value = "shoplist")
    public String shopList() {
        return "shoplist";
    }

    @RequestMapping(value = "shopsearch")
    public String shopSearch() {
        return "shopsearch";
    }

    @RequestMapping(value = "selectshop")
    @ResponseBody
    public Shop selectShopById(@RequestParam("sid") Integer sid) {
        return shopService.selectShopById(sid);
    }

    @ResponseBody
    @RequestMapping("allshop")
    public List<Shop> selectAllShop() {
        return shopService.selectAllShop();
    }

    @RequestMapping(value = "addshop")
    public String addShop(MultipartFile file, HttpServletRequest request, Model model) throws IOException {
        String name = request.getParameter("name");
        String intro = request.getParameter("intro");
        String priceStr = request.getParameter("price");
        String sidStr = request.getParameter("sid");
        int sid = 0;
        if (sidStr!=null){
            sid = Integer.valueOf(sidStr);
        }

        int price = 0;
        if (!priceStr.isEmpty()) {
            price = Integer.valueOf(priceStr);
        }

        Shop shop = new Shop();
        shop.setSname(name);
        shop.setSintro(intro);
        shop.setSprice(price);
        shop.setFile(file);
        //保存数据库的路径
        String sqlPath = null;
        //定义文件保存的本地路径
        String localPath = "/Users/dllo/Desktop/frame/GameApp/src/main/webapp/images/";
        String targetPath = "/Users/dllo/Desktop/frame/GameApp/target/GameApp/images/";
        //定义 文件名
        String filename = null;
        if (shop.getFile()!=null) {
            //生成uuid作为文件名称
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            //获得文件类型（可以判断如果不是图片，禁止上传）
            String contentType = shop.getFile().getContentType();
            //获得文件后缀名
            String suffixName = contentType.substring(contentType.indexOf("/") + 1);
            //得到 文件名
            filename = uuid + "." + suffixName;
            //文件保存路径
            shop.getFile().transferTo(new File(localPath + filename));
            CopyPic.copyFile(localPath+filename,targetPath+filename);
            //把图片的相对路径保存至数据库
            sqlPath = "/images/" + filename;
        }else {
            sqlPath = shopService.selectShopById(sid).getSpic();
        }

        shop.setSpic(sqlPath);
        if (sid > 0 ) {
            request.getSession().setAttribute("name", name);
            request.getSession().setAttribute("intro", intro);
            request.getSession().setAttribute("price", price);
            request.getSession().setAttribute("pic", sqlPath);
            return "redirect:updateshop?sid=" + sid + "";
        }
        int result = shopService.insertShop(shop);
        model.addAttribute("shop", shop);
        if (result > 0) {
            request.getSession().setAttribute("success", "success");
        } else {
            request.getSession().setAttribute("fail", "fail");
        }
        return "redirect:shoplist";
    }


    @RequestMapping(value = "updateshop")
    public String updateShop(HttpServletRequest request, Model model) throws IOException {
        String name = (String) request.getSession().getAttribute("name");
        String intro = (String) request.getSession().getAttribute("intro");
        int price = (Integer) request.getSession().getAttribute("price");
        String pic = (String) request.getSession().getAttribute("pic");
        request.getSession().removeAttribute("name");
        request.getSession().removeAttribute("intro");
        request.getSession().removeAttribute("price");
        request.getSession().removeAttribute("pic");

        int sid = Integer.valueOf(request.getParameter("sid"));

        Shop shop = new Shop();
        shop.setSid(sid);
        shop.setSname(name);
        shop.setSintro(intro);
        shop.setSprice(price);
        shop.setSpic(pic);

        int result = shopService.updateShop(shop);
        model.addAttribute("shop", shop);
        if (result > 0) {
            request.getSession().setAttribute("success", "success");
        } else {
            request.getSession().setAttribute("fail", "fail");
        }
        return "redirect:shoplist";
    }

    @RequestMapping(value = "delshop")
    public String deleteShopById(HttpServletRequest request, @RequestParam("sid") Integer sid) {
        int result = shopService.deleteShopById(sid);
        if (result > 0) {
            request.getSession().setAttribute("success", "success");
        } else {
            request.getSession().setAttribute("fail", "fail");
        }
        return "redirect:shoplist";
    }

    @ResponseBody
    @RequestMapping(value = "searchshopname")
    public List<Shop> searchname(@Param("name")String name){
        List<Shop> shopList = shopService.selectShopByName(name);
        return shopList;
    }
}
